//
// Created by csy on 2023/5/30.
//

#ifndef TINYTINY_RPC_TIMER_EVENT_H
#define TINYTINY_RPC_TIMER_EVENT_H
#include <functional>
#include <memory>

namespace rocket {

  class TimerEvent {
  public:
    typedef std::shared_ptr<TimerEvent> ptr;

  public:
    struct Compare {
      bool operator()(const TimerEvent::ptr& le, const TimerEvent::ptr& ri) const {
        return le->getArriveTime() < ri->getArriveTime();
      }
    };

    TimerEvent(int interval, const std::function<void()>& cb, bool is_repeated = false);

    int64_t getArriveTime() const { return _arrive_time; }
    void setCancled(bool value) { _is_cancled = value; }
    bool isCancled() { return _is_cancled; }
    bool isRepeated() { return _is_repeated; }
    std::function<void()> getCallBack() { return _task; }
    void resetArriveTime();

  private:
    int64_t _arrive_time;    // ms
    int64_t _interval;       // ms
    bool _is_repeated {false};
    bool _is_cancled {false};

    std::function<void()> _task;
  };
}
#endif //TINYTINY_RPC_TIMER_EVENT_H